//index.js
//获取应用实例
const app = getApp()
let axios = require('../../../utils/httpUtil.js')
let _map = require('../../../utils/map.js')
let _login = require('../../../utils/login.js')

Page({
    data: {
        nowData: '',
        nowLat: "",
        nowLng: "",
        selAddr: '',
        addressList: [],
        fujinList: [

        ],
        noAddress: false
    },
    relocal: function() {
        this.setData({
            nowData: '正在定位中...'
        })
        this.getAddressLoac();
        console.log(111);
    },
    addAddress() {
		var uu = wx.getStorageSync('uid');

		console.log(uu);
		if (!uu){
			return;
		}
        //添加新地址
        wx.navigateTo({
            url: '../../self/addRess/addRess'
        })
    },
    loginx: function() {
        var _this = this;
		if(!wx.getStorageSync('uid')){
			_login.loginx(this, function() {
				_this.addAddress();
			});
		}
    },
    getInfo() {
        let self = this;
        wx.getStorage({
            key: 'uid',
            success(res) {
                console.log(res)
                axios.reqPost('xwx/getUserAddressList', {
                    userId: res.data, //res.data
                    pageNum: 1,
                    pageSize: 10
                }, (info) => {
                    if (info.resultCode == 1000) {
                        if (info.returnObject) {
                            self.setData({
                                addressList: info.returnObject.list
                            })
                        } else {
                            wx.showToast({
                                title: '您还没有收货地址哦',
                                icon: 'none'
                            })
                            self.setData({
                                noAddress: true
                            })
                        }
                    } else {
                        wx.showToast({
                            title: '网络不好哦',
                        })
                    }
                })
            }
        })
    },
    selLoact: function(e) {
        var that = this;
        console.log(e);
        var lat = e.currentTarget.dataset.lat;
        var lng = e.currentTarget.dataset.lng;
        var addr = e.currentTarget.dataset.addr;

        wx.setStorage({
            key: 'location',
            data: {
                "lat": lat,
                'lng': lng
            },
            success: function(res) {
                console.log(res);
                var pages = getCurrentPages(); // 获取页面栈
                var currPage = pages[pages.length - 1]; // 当前页面
                var prevPage = pages[pages.length - 2]; // 上一个页面
                console.log(that);
                prevPage.setData({
                    optionsAdd: addr
                });
                wx.navigateBack({
                    delta: 1
                })
                return;
                wx.switchTab({
                    url: '/pages/home/index', //注意switchTab只能跳转到带有tab的页面，不能跳转到不带tab的页面
                    success: function(e) {
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        page.setData({
                            optionsAdd: addr
                        })
                        page.onLoad();
                    }
                })
            }
        })
    },
    getAddressLoac: function() {
        var _this = this;
        _map.getLocation(function(res) {
            _map.reverseGeocoder(res.latitude, res.longitude, function(res) {
                _this.setData({
                    nowData: res.formatted_addresses.recommend,
                    nowLat: res.location.lat,
                    nowLng: res.location.lng
                })
            });
            _map.getAddressList(res.latitude, res.longitude, function(res) {
                console.log(res);
                _this.setData({
                    fujinList: res
                })
            });
        });
    },
    getAddrList: function() {

    },
    onLoad: function() {
        this.getInfo();
        this.getAddressLoac();
    },
	onShow:function(){
		this.getInfo();
		this.getAddressLoac();
	}

})